/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.controller<br/>  
 * <b>文件名：</b>ServiceOrgController.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年10月8日 下午7:13:52<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.controller;

import com.alibaba.fastjson.JSON;
import com.flea.system.base.util.StringUtil;
import com.flea.system.dao.OrgCertificationMapper;
import com.flea.system.entity.OrgUser;
import com.flea.system.service.IOrgCertificationService;
import com.flea.system.service.IOrgUserService;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.flea.hclv.service.HServiceOrgService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.ControllerResult;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description 服务组织机构
 * @createTime 2018年10月8日 下午7:13:52
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Controller
@RequestMapping("/flea/hclv/serviceOrg")
public class HServiceOrgController {
	@Autowired
	private HServiceOrgService serviceOrgService;
	@Autowired
	private IOrgUserService iOrgUserService;
	@Autowired
	private IOrgCertificationService iOrgCertificationService;
	@RequestMapping("/saveOrUpdate")
	public void saveOrUpdate(HttpServletRequest request,@RequestParam("model") String objModel,HttpServletResponse response) {
		boolean result = serviceOrgService.saveOrUpdate(request,objModel);
		ControllerResult cResult = new ControllerResult(result);
        cResult.WriteToPage(response);
	}
	@RequestMapping("/saveOrUpdateDeptMent")
	public void saveOrUpdateDeptMent(HttpServletRequest request,@RequestParam("model") String objModel,HttpServletResponse response) {
		boolean result = serviceOrgService.saveOrUpdateDeptMent(request,objModel);
		ControllerResult cResult = new ControllerResult(result);
		cResult.WriteToPage(response);
	}
	/**
	 * 
	 * @description 不限定权限，平台获取用户获取所有的在系统中的服务组织
	 * @methodName queryAllServiceOrgPage
	 * @param
	 * @returnType void   
	 * @throw
	 */
	@RequestMapping("/queryAllServiceOrgPage")
	public void queryAllServiceOrgPage(HttpServletRequest request, FleaPage<OrgDept> paging,HttpServletResponse response) {
		String queryName = request.getParameter("queryName");
		String serviceType = request.getParameter("serviceType");
		String queryProvince = request.getParameter("queryProvince");
		String queryCity = request.getParameter("queryCity");
		String queryZone = request.getParameter("queryZone");
		String querySatus = request.getParameter("deleted");

		FleaPage<OrgDept> pageList = serviceOrgService.findForGird(queryName,serviceType, queryProvince, queryCity, queryZone,querySatus, paging.getCurPage(), paging.getPageSize());

		ControllerResult conresult = new ControllerResult(pageList);
		conresult.set(true, "操作成功");

		conresult.WritePagingDataToPage(response, pageList);
	}
	@RequestMapping("deletByIds")
	public void deletServiceOrg(HttpServletRequest request,
			@Param("ids") String ids, HttpServletResponse response) {

		String userId = FleaOrgUtils.getCurUserUuid(request);
		boolean result = serviceOrgService.deleteByIds(ids, userId);

		ControllerResult cResult = new ControllerResult(result,
				result == true ? "操作成功" : "操作失败", result);
		cResult.WriteToPage(response);
	}
	@RequestMapping("fronze")
	public void fronze(HttpServletRequest request,@Param("ids") String ids, HttpServletResponse response) {
		
		String userId = FleaOrgUtils.getCurUserUuid(request);
    List<String>  idsArray = JSON.parseArray(ids, String.class);
		boolean result = serviceOrgService.updateDeletedFrozon(ids, userId);
    for (String deptId: idsArray ) {
      List<OrgUser> users = iOrgUserService.findByDeptUuid(deptId);
      boolean results =  iOrgCertificationService.updateUserToLockStatus(users);
    }

		ControllerResult cResult = new ControllerResult(result,
				result == true ? "操作成功" : "操作失败", result);
		cResult.WriteToPage(response);
	}
	@RequestMapping("cancelFronze")
	public void cancelFronze(HttpServletRequest request,@Param("ids") String ids, HttpServletResponse response) {
		
		String userId = FleaOrgUtils.getCurUserUuid(request);
    List<String>  idsArray = JSON.parseArray(ids, String.class);
		boolean result = serviceOrgService.updateDeletedcalFrozon(ids, userId);
    for (String deptId: idsArray ) {
      List<OrgUser> users = iOrgUserService.findByDeptUuid(deptId);
      boolean results =  iOrgCertificationService.updateUserToUnLockStatus(users);
    }
		ControllerResult cResult = new ControllerResult(result,
				result == true ? "操作成功" : "操作失败", result);
		cResult.WriteToPage(response);
	}
	@RequestMapping("getAsyncDeptTreeForAuth")
	public void getAsyncDeptTreeForAuth(HttpServletRequest request, HttpServletResponse response) {
			  List<OrgDept> result = serviceOrgService.findeByExtend2(request);

	        ControllerResult controllerResult = new ControllerResult();
	        controllerResult.WriteDataToPage(response, result);
	}
	/**
	 * 获取我我的所有的合作伙伴列表
	 * @description
	 * @methodName queryAllServicePatnerPage
	 * @param
	 * @returnType void   
	 * @throw
	 */
	@RequestMapping("/queryAllServicePatnerPage")
	public void queryAllServicePatnerPage(HttpServletRequest request, FleaPage<OrgDept> paging,HttpServletResponse response) {
		String queryName = request.getParameter("queryName");
		String serviceType = request.getParameter("serviceType");
		String queryProvince = request.getParameter("queryProvince");
		String queryCity = request.getParameter("queryCity");
		String queryZone = request.getParameter("queryZone");
		String querySatus = request.getParameter("deleted");
		String serviceId = FleaOrgUtils.getCurrentUserDeptUuid(request);
		FleaPage<OrgDept> pageList = serviceOrgService.findPatnersForGird(serviceId,queryName,serviceType, queryProvince, queryCity, queryZone,querySatus, paging.getCurPage(), paging.getPageSize());

		ControllerResult conresult = new ControllerResult(pageList);
		conresult.set(true, "操作成功");

		conresult.WritePagingDataToPage(response, pageList);
	}
	/**
	 * 根据服务组织结构名称获取组织机构
	 * @description
	 * @methodName queryAllServicePatnerPage
	 * @param
	 * @returnType void   
	 * @throw
	 */
	@RequestMapping("/getServiceOrgByName")
  @ResponseBody
	public ServerResponse<List<OrgDept>> getServiceOrgByName(HttpServletRequest request,@RequestParam("model")String deptName) {

		List<OrgDept> result = FleaOrgUtils.getDeptByLikeName(deptName);
		
		return ServerResponse.createBySuccess(result);
	}

  /**
   * 根据部门获取该部门的所有用户信息
   * @param request
   * @param paging
   * @return
   */
	@ResponseBody
  @RequestMapping("/getOrgUserByDeptId")
	public ServerResponse getOrgUserByDeptId(HttpServletRequest request,FleaPage paging){
	  String deptId = request.getParameter("model");
	  if(StringUtil.isEmpty(deptId)){
      return ServerResponse.createByErrorMessage("部门信息为空，请确认参数信息");
    }
    FleaPage<OrgUser> users =   iOrgUserService.pagingByDeptUuid(deptId,paging.getCurPage(),paging.getPageSize());
    return ServerResponse.createBySuccess(users);
	}
  @ResponseBody
  @RequestMapping("/freeZoenUserByDeptId")
  public ServerResponse freeZoenUserByDeptId(HttpServletRequest request){
    String deptId = request.getParameter("model");
    List<OrgUser> users = iOrgUserService.findByDeptUuid(deptId);
    boolean result =  iOrgCertificationService.updateUserToLockStatus(users);
	  return null;
  }



}
